package ru.volgogradetzzz.ik
{
	import flash.display.Graphics;
	import ru.volgogradetzzz.math.VMatrix2x2;
	import ru.volgogradetzzz.math.VVector2;
	/**
	 * ...
	 * @author volgogradetzzz
	 */
	public class VBone 
	{
		public var next:VBone;
		public var prev:VBone;
		
		public var position:VVector2 = new VVector2();
		public var direction:VVector2 = new VVector2();
		
		private var _orientation:VMatrix2x2 = new VMatrix2x2();
		private var _length:Number = 0;
		private var _initialDirection:VVector2 = new VVector2();
		
		
		public function VBone(length:Number, ang:Number = 0) 
		{
			_length = length;
			_initialDirection.x = _length;
			
			var m:VMatrix2x2 = new VMatrix2x2();
			m.prepareForRotation(ang);
			rotate(m);
		}
		
		public function rotate(m:VMatrix2x2):void
		{
			_orientation.multiplyByMatrix(m);
			direction = _orientation.multiplyByVector(_initialDirection);
			
			if (next)
			{
				next.position = position.addReturn(direction);
				next.rotate(m);
			}
		}
		private var color:uint = Math.random() * 0xFFFFFF;
		public function render(g:Graphics):void
		{
			g.lineStyle(2, color);
			g.moveTo(position.x, position.y);
			g.lineTo(position.x + direction.x, position.y + direction.y);
			g.lineStyle();
		}
		
		public function get length():Number 
		{
			return _length;
		}
		
		public function setPosition(value:VVector2):void
		{
			position = value.clone();
			
			if (next)
			{
				next.position = position.addReturn(direction);
			}
		}
	}
}